// Created from GroupGenerationTemplate
// For Decisions HL7 Engine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using DecisionsFramework.Design.Properties;
using Decisions.HL7.DataTypes;
using ST = System.String;
using SI = System.String;
using IS = System.String;
using ID = System.String;
using SNM = System.String;

namespace Decisions.HL7.Message
{
    [DataContract]
	public class PATIENT_CRM_C01 : AbstractGroup
    {
        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("3 PID",3)]
  	    [HL7Field(HL7Version.v23, 3, HL7Repeat.False, HL7Required.True,"PID")]
	    public PID PID {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("5 PV1",5)]
  	    [HL7Field(HL7Version.v23, 5, HL7Repeat.False, HL7Required.False,"PV1")]
	    public PV1 PV1 {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("7 CSR",7)]
  	    [HL7Field(HL7Version.v23, 7, HL7Repeat.False, HL7Required.True,"CSR")]
	    public CSR CSR {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("10 CSP",10)]
  	    [HL7Field(HL7Version.v23, 10, HL7Repeat.True, HL7Required.False,"CSP")]
	    public CSP[] CSP {get;set;}



		#region Parser Helpers and Conrete Impls

        internal override void ConsumeTextSegmentsForGroup(Stack<string> availableData, HL7ParserOptions options) {

			PID = PopSegmentParseAndCheckRequired<PID>(availableData, options, "PID");
			PV1 = PopSegmentParseAndCheckRequired<PV1>(availableData, options, "PV1");
			CSR = PopSegmentParseAndCheckRequired<CSR>(availableData, options, "CSR");
			CSP = PopSegmentParseAndCheckRequiredArray<CSP>(availableData, options, "CSP");
        }

		internal override bool CanConsume(string availableData, HL7ParserOptions options)
        {
			bool isPID = availableData.StartsWith("PID", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("PID", options.Version) || isPID) {
				return isPID;
			}
			bool isPV1 = availableData.StartsWith("PV1", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("PV1", options.Version) || isPV1) {
				return isPV1;
			}
			bool isCSR = availableData.StartsWith("CSR", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("CSR", options.Version) || isCSR) {
				return isCSR;
			}
			bool isCSP = availableData.StartsWith("CSP", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("CSP", options.Version) || isCSP) {
				return isCSP;
			}
			return false;
        }

        private static Dictionary<string, bool> SEGMENT_VERSION_REQUIRED_MAP = new Dictionary<string, bool>();

		protected override Dictionary<string, bool> MAP_OF_SEGMENT_TO_REQUIRED
        {
            get
			{
				return SEGMENT_VERSION_REQUIRED_MAP;
			}
        }

		public override string HL7ENTITY_TYPE_NAME
        {
            get {
				return "PATIENT";
			}
        }

        static PATIENT_CRM_C01() {

             SetupStaticPropertyMap(SEGMENT_VERSION_REQUIRED_MAP, typeof(PATIENT_CRM_C01));

        }

		#endregion

		public override string GetER7(HL7ParserOptions options)
		{
			List<string> segmentData = new List<string>();
		 if (PID != null) {
			string segmentValue = PID.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (PV1 != null) {
			string segmentValue = PV1.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (CSR != null) {
			string segmentValue = CSR.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (CSP != null && CSP.Length > 0) {
			for (int i = 0; i < CSP.Length; i++) {
				string segmentValue = CSP[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
           return string.Join(HL7ParserConstants.SEGMENT_SEPARATOR, segmentData.ToArray());
        
        }
    }
}

